<template>
  <div class="Area">
    <!--    <el-table-->
    <!--        :data="tableData.filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase()))"-->
    <!--        type="index"-->
    <!--        style="width: 100%">-->
    <el-table style="width: 100%;"
              :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)"
    >
      <el-table-column
          type="index">
      </el-table-column>
      <el-table-column
          label="开始日期"
          prop="sBegin">
      </el-table-column>
      <el-table-column
          label="开始时间"
          prop="sBegindate">
      </el-table-column>
      <el-table-column
          label="结束日期"
          prop="sEnd">
      </el-table-column>
      <el-table-column
          label="结束时间"
          prop="sEnddate">
      </el-table-column>
      <el-table-column
          label="秒杀价"
          prop="sPrice">
      </el-table-column>
      <el-table-column
          label="限购数量"
          prop="sLimit">
      </el-table-column>
      <el-table-column
          label="秒杀状态"
          prop="status">
      </el-table-column>
      <el-table-column>
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.$index, scope.row)" type="primary" style="margin-left: 16px;" size="mini">
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pagesize"
        layout="total, prev, pager, next, jumper"
        :total="tableData.length">
    </el-pagination>

    <el-drawer
        title="详情配置"
        :visible.sync="drawer"
        :with-header="true">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="商品图片" prop="mImg">
          <el-image :src="ruleForm.rjbCommodity.mImg" style="width: 200px; height: 200px"></el-image>
        </el-form-item>
        <el-form-item label="商品名称" prop="mName">
          <el-input v-model="ruleForm.rjbCommodity.mName" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="限购数量" prop="sLimit">
          <el-input v-model="ruleForm.sLimit"></el-input>
        </el-form-item>
        <el-form-item label="秒杀规则" prop="rId">
          <el-input v-model="ruleForm.rId"></el-input>
        </el-form-item>
        <el-form-item label="商品库存" prop="mInstock">
          <el-input v-model="ruleForm.rjbCommodity.mInstock" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="mPromotion">
          <el-input v-model="ruleForm.rjbCommodity.mPromotion" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="秒杀价格" prop="sPrice">
          <el-input v-model="ruleForm.sPrice"></el-input>
        </el-form-item>
        <el-form-item label="秒杀状态" prop="status">
          <el-input v-model="ruleForm.status"></el-input>
        </el-form-item>
        <el-form-item label="开始时间" required>
          <el-col :span="11">
            <el-form-item prop="sBegin">
              <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.sBegin"
                              style="width: 100%;"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col class="line" :span="2">-</el-col>
          <el-col :span="11">
            <el-form-item prop="sBegindate">
              <el-time-picker placeholder="选择时间" v-model="ruleForm.sBegindate" style="width: 100%;"></el-time-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="结束时间" required>
          <el-col :span="11">
            <el-form-item prop="sEnd">
              <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.sEnd"
                              style="width: 100%;"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col class="line" :span="2">-</el-col>
          <el-col :span="11">
            <el-form-item prop="sEnddate">
              <el-time-picker placeholder="选择时间" v-model="ruleForm.sEnddate" style="width: 100%;"></el-time-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
        <!--        <el-form-item>
                  <el-button type="primary" @click="submitForm(ruleForm)">提交</el-button>
                </el-form-item>-->
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1, //初始页
      pagesize: 10,    //    每页的数据
      ruleForm: {
        rjbCommodity: {
          mName: '',
          sLimit: '',
          mInstock: '',
          mPromotion: '',
          mImg: '',
        },
        delivery: false,
        type: [],
        resource: '',
        desc: '',
        rId: '',
        sPrice: '',
        sBegin: '',
        sBegindate: '',
        sEnd: '',
        sEnddate: '',
      },
      rules: {
        sbegin: [
          {type: 'date', required: true, message: '请选择日期', trigger: 'change'}
        ],
        sbegindate: [
          {type: 'date', required: true, message: '请选择时间', trigger: 'change'}
        ],
        send: [
          {type: 'date', required: true, message: '请选择日期', trigger: 'change'}
        ],
        senddate: [
          {type: 'date', required: true, message: '请选择时间', trigger: 'change'}
        ],
      },
      drawer: false,
      tableData: [{
        sBegin: '',
        sBegindate: '',
        sBnd: '',
        sBnddate: '',
        sLimit: '',
        sPrice: '',
        mId: '',
        rId: '',
        sId: '',
        status: '',
      }],
      search: '',
      query: {
        pageNum: 1,
        pageSize: 10,
        startDate: '',
        endDate: ''
      }
    }
  },
  mounted() {
    this.getTableData();
  },
  methods: {
    // 初始页currentPage、初始每页数据数pagesize和数据data
    handleSizeChange: function (size) {
      this.pagesize = size;
      // console.log(this.pagesize)  //每页下拉显示数据
    },
    handleCurrentChange: function (currentPage) {
      this.currentPage = currentPage;
      // console.log(this.currentPage)  //点击第几页
    },
    success() {
      this.$notify({
        title: '成功',
        message: '修改成功',
        type: 'success'
      });
    },
    failure() {
      this.$notify.error({
        title: '错误',
        message: '修改失败'
      });
    },
    // 添加表格数据
    getTableData() {
      this.$http({
        method: 'get',
        url: "rjbSpkie/queryByList",
        /*params: this.query,*/
        dataType: "json"
      })
          .then(data => {
            // console.log(data.data);
            let myList = data.data;
            myList = myList.sort(function (a, b) {
              return b.sEnd > a.sEnd ? 1 : -1
            })
            this.tableData = myList;
            for (let i = 0; i < data.data.length; i++) {
              let sbeginDate = this.timeComparison(data.data[i].sBegin + " " + data.data[i].sBegindate); // 开始时间
              //console.log("开始时间" + data.data[i].sBegin + " " + data.data[i].sBegindate);
              let strDate = this.timeComparison(data.data[i].sEnd + " " + data.data[i].sEnddate); // 结束时间
              //console.log("结束时间" + data.data[i].sEnd + " " + data.data[i].sEnddate);
              //console.log(this.isDuringDate(data.data[i].sBegin + " " + data.data[i].sBegindate, data.data[i].sEnd + " " + data.data[i].sEnddate));
              if (sbeginDate === "剩余还有时间" && strDate === "剩余还有时间") {
                this.tableData[i].status = '活动待开始';
              } else if (sbeginDate === "没有时间" && strDate === "剩余还有时间") {
                this.tableData[i].status = '活动正在进行中';
              } else {
                this.tableData[i].status = '活动已结束';
              }
            }
            //console.log(this.tableData);
            // let s = this.timeComparison(data.data[0].sbegin + " " + data.data[0].sbegindate);
          })
          .catch(error => {
            console.log(error);
          });
    },
    // 时间比较
    timeComparison(customTime) {
      //获取当前时间
      let currentTime = new Date();
      customTime = customTime.replace("-", "/");//替换字符，变成标准格式
      customTime = new Date(Date.parse(customTime));
      if (customTime > currentTime) return "剩余还有时间";
      if (customTime === currentTime) return "时间相同";
      if (customTime < currentTime) return "没有时间";
      // return currentTime < customTime; // 传入时间是否大于当前时间
    },
    // 时间比较
    isDuringDate(beginDateStr, endDateStr) {
      /** [beginDateStr] [开始时间]
       * [endDateStr]   [结束时间]
       * */
      var curDate = new Date(),
          beginDate = new Date(beginDateStr),
          endDate = new Date(endDateStr);
      if (curDate >= beginDate && curDate <= endDate) {
        return true;
      }
      return false;
    },
    // from校验
    submitForm(formName) {
      if (formName.status === "活动待开始" || formName.status === "活动已结束") {
        this.drawer = false;
        this.success();
      } else {
        this.failure();
      }
    },
    //详情按钮
    handleEdit(index, row) {
      this.drawer = true;
      this.ruleForm = row;
      // console.log(row);
    },
  },
}
</script>

<style scoped>
.Area {
  width: 100%;
  min-height: 100%;
  padding: 15px;
  box-sizing: border-box;
}
</style>
